﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;

using System.Net.NetworkInformation;
using System.Xml.Linq;

/* Usage:
 * create new Feed
 * Download it with StartDownload
 * Load it whit LoadFeed
 * 
            Feed f1 = new Feed("ts", "http://4jesoloevents.it/feed/eventi/jesolo.xml");
            f1.StartDownload();
            try
            {
                result.Text = f1.LoadFeed().ToString();
            }
            catch
            {
                MessageBox.Show("Data could not be found", "No Local file found", MessageBoxButton.OK);
            }
 */

namespace AugmentedReality
{
    public class Feed
    {
        //variable
        public String name;
        public String url;
        public XDocument loadedData;

        public Feed(String name, String url) {
            this.name = name;
            this.url = url;
        }

        //Save the feed with the name
        private XDocument SaveFeed(String arg)
        {
            IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream stream = iso.CreateFile(name + ".xml");
            StreamWriter streamWrite = new StreamWriter(stream);
            streamWrite.Write(arg);
            streamWrite.Close();
            XDocument loadedData = XDocument.Parse(LoadFeed().ToString(), LoadOptions.None);
            return loadedData;
        }

        //load feed and return a XDocument
        public XDocument LoadFeed()
        {
            IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
            if (iso.FileExists(name + ".xml"))
            {
                //read the feed form the file
                IsolatedStorageFileStream stream = iso.OpenFile(name + ".xml", System.IO.FileMode.Open);
                StreamReader streamReader = new StreamReader(stream);
                String s = streamReader.ReadToEnd();
                XDocument loadedData = XDocument.Parse(s, LoadOptions.None);
                streamReader.Close();
                return loadedData; //return the loaded Feed
            }
            else
            {
                MessageBox.Show("Could not retrive data", "Error", MessageBoxButton.OK);
                throw new System.ArgumentException("Could not retrive data", "Error");
            }
        }

        // download the feed from the url (e.g. http://www.test.it/feed.xml)
        // or load it from local storage
        public void StartDownload()
        {
            if (NetworkInterface.GetIsNetworkAvailable()) //Is internet Available?
            {
                WebClient wc = new WebClient();
                wc.DownloadStringCompleted += HttpsCompleted;
                wc.DownloadStringAsync(new Uri(url));
            }
            else
            {
                MessageBox.Show("Data could not be updated", "Internet connection not available", MessageBoxButton.OK);
            }
        }

        // When download is complete
        private void HttpsCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None);
                XDocument loadedData = XDocument.Parse(e.Result);
                String allFeed = e.Result; //save the feed on a variable
                SaveFeed(allFeed);
            }
        }

        public String Name()
        {
            return name;
        }

        public String Url()
        {
            return url;
        }

    }
}
